<?php
$UnitsObj = new Unit();
$ImagesObj = new Image();
$ConfigObj = new GeneralConfig();
$CustomersObj = new Customer();

function get_unit_details(){
	global $UnitsObj, $ImagesObj;
	$AmenityObj = new Amenity();
	$BookingsObj = new Booking();
	$confirmed_dates = array();
	$unconfirmed_dates = array();
	
	$Unit = $UnitsObj->find((int)$_GET['PropertyId']);
	
	// get confirmed date
	$bookings = $BookingsObj->select_row_query("SELECT * FROM `".$BookingsObj->get_table_name()."` WHERE `uid` = '$Unit->id' AND `confirmed` = 1", "");
	foreach ($bookings as $booking){
		$startdate = get_string_from_date($booking->startdate);
		$enddate = add_date( get_string_from_date($booking->enddate), 1 );
		for($i = $startdate; $i<$enddate; $i = add_date( $i, 1 )){
			array_push( $confirmed_dates, $i );
		}
	}
	$confirmed_dates = implode(",", $confirmed_dates);
	// get not confirmed dates
	$bookings = $BookingsObj->select_row_query("SELECT * FROM `".$BookingsObj->get_table_name()."` WHERE `uid` = '$Unit->id' AND `confirmed` = 0", "");
	foreach ($bookings as $booking){
		$startdate = get_string_from_date($booking->startdate);
		$enddate = add_date( get_string_from_date($booking->enddate), 1 );
		for($i = $startdate; $i<$enddate; $i = add_date( $i, 1 )){
			array_push( $unconfirmed_dates, $i );
		}
	}
	$unconfirmed_dates = implode(",", $unconfirmed_dates);
	
	$Images = $ImagesObj->select_row_query("SELECT * FROM `".$ImagesObj->get_table_name()."` WHERE `uid`='$Unit->id' ORDER BY `order` ASC", "");
	$AmenitiesIds = explode(",", $Unit->amenities);
	$Amenities = array();
	foreach ($AmenitiesIds as $id){
		$Amenity = $AmenityObj->find((int)$id);
		array_push($Amenities, $Amenity->name);
	}
	
	$PricesObj = new Price();
	$Prices = $PricesObj->find(array('uid'=>$Unit->id));
	include_once 'views/display_unit_details.html.php';
}

function get_unit_list(){
	global $UnitsObj, $ImagesObj;
	$PricesObj = new Price();
	
	if($_POST){
		switch ($_POST['sort']){
			case "price":
				$units = $UnitsObj->select_row_query("SELECT t1. * , min( t2.price ) AS min_price FROM `".$UnitsObj->get_table_name()."` AS t1 LEFT JOIN `".$PricesObj->get_table_name()."` AS t2 ON t2.uid = t1.id GROUP BY t1.id ORDER BY min_price ASC", "");
				break;
			case "people":
				$units = $UnitsObj->select_row_query("SELECT t1. * , min( t2.price ) AS min_price FROM `".$UnitsObj->get_table_name()."` AS t1 LEFT JOIN `".$PricesObj->get_table_name()."` AS t2 ON t2.uid = t1.id GROUP BY t1.id ORDER BY t1.maximum_occupancy DESC", "");
				break;
			case "city":
				$units = $UnitsObj->select_row_query("SELECT t1. * , min( t2.price ) AS min_price FROM `".$UnitsObj->get_table_name()."` AS t1 LEFT JOIN `".$PricesObj->get_table_name()."` AS t2 ON t2.uid = t1.id GROUP BY t1.id ORDER BY t1.city ASC", "");
				break;
			case "country":
				$units = $UnitsObj->select_row_query("SELECT t1. * , min( t2.price ) AS min_price FROM `".$UnitsObj->get_table_name()."` AS t1 LEFT JOIN `".$PricesObj->get_table_name()."` AS t2 ON t2.uid = t1.id GROUP BY t1.id ORDER BY t1.country ASC", "");
				break;
		}
	}
	else{
//		$conditions = array("published"=>1);
		if( $_GET['category'] ) $conditions = " AND t1.category = '".$_GET['category']."'";
		if( $_GET['city'] ) $conditions = " AND t1.city = '".$_GET['city']."'";
//		$units = $UnitsObj->find($conditions);
		$units = $UnitsObj->select_row_query("SELECT t1. * , min( t2.price ) AS min_price FROM `".$UnitsObj->get_table_name()."` AS t1 LEFT JOIN `".$PricesObj->get_table_name()."` AS t2 ON t2.uid = t1.id WHERE t1.published = 1". $conditions ." GROUP BY t1.id", "");
	}
	
	$thumbs = array();
//	$prices = array();
	foreach ($units as $unit){
		$image = $ImagesObj->select_row_query("SELECT `thumbnailurl` FROM `".$ImagesObj->get_table_name()."` WHERE `uid` = '$unit->id' ORDER BY `order` ASC LIMIT 0,1", 1);
//		$price = $PricesObj->select_row_query("SELECT `price` FROM `".$PricesObj->get_table_name()."` WHERE `uid` = '$unit->id' ORDER BY `price` ASC LIMIT 0, 1", 1);
		array_push($thumbs, $image->thumbnailurl);
//		array_push($prices, $price->price);
	}
	$no_of_chars_in_desc = 150;
	include_once 'views/display_units.html.php';
}

function send_enquiry_form(){
	global $notice, $ConfigObj;
	
	$config = $ConfigObj->find(1);
	$notice['error'] = 1;
	
	$arrival_date = $_POST['enquiry']['startdate']['month']."/".$_POST['enquiry']['startdate']['date']."/".$_POST['enquiry']['startdate']['year'];
	$departure_date = $_POST['enquiry']['enddate']['month']."/".$_POST['enquiry']['enddate']['date']."/".$_POST['enquiry']['enddate']['year'];
	
	if( !filter_var($_POST['enquiry']['email'], FILTER_VALIDATE_EMAIL) ){
		$notice['msg'] = "Please enter a valid E-mail address";
	}
	else if( !filter_var($_POST['enquiry']['telephone'], FILTER_VALIDATE_INT) ){
		$notice['msg'] = "Please enter a valid telephone containing only numbers";
	}
	else if( $arrival_date == $departure_date ){
		$notice['msg'] = "Arrival date and return date cannot be the same";
	}
	/*else if(){
		// check for captcha here
	}*/
	else{
		send_enquiry($config->admin_email, $_POST['enquiry']);
	}
}

function identify_owner(){
	global $my, $CustomersObj;
	
	$manager = $CustomersObj->select_row_query("SELECT * FROM `".$CustomersObj->get_table_name()."` WHERE `joomla_id` = '$my->id'", 1);
	return $manager;
}

function get_unit_ids($manager = ""){
	if( $manager == "" ) $manager = identify_owner();
	global $UnitsObj;
	$ids = array();

	$units = $UnitsObj->select_row_query("SELECT `id` FROM `".$UnitsObj->get_table_name()."` WHERE `owner_id` = '$manager->id'", "");
	foreach ($units as $unit){
		array_push($ids, $unit->id);
	}
	return $ids;
}

function start_component(){
	include_once 'views/user_component_start.html.php';
}

function end_component(){
	global $my, $KToolBar;
	include_once 'views/user_component_end.html.php';
}

function show_notice(){
	global $notice;
	include_once 'views/user_show_notice.html.php';
}
?>